package wtws.message;

import java.nio.ByteBuffer;
import java.util.ArrayList;

import wtws.message.detail.GraphicMicroBustProductSubBlock;
import wtws.message.interf.TdwrProdMessage;
import wtws.util.ByteUtil;

public class GraphicMicrobustProduct implements TdwrProdMessage {

	private static final long serialVersionUID = 1L;
	public static final int MSG_ID = 9235;
	public static final String TITLE = "Graphic Microbust Product";
	public static final int MSG_LEN = 12;

	private int messageId;
	private int messageLength;
	private long mbProductGenerationTime;
	private int numberOfShapes;
	private ArrayList<GraphicMicroBustProductSubBlock> subBlock;

	public GraphicMicrobustProduct(long mbProductGenerationTime, ArrayList<GraphicMicroBustProductSubBlock> subBlock) {
		this.messageId = MSG_ID;
		this.subBlock = subBlock;
		this.mbProductGenerationTime = mbProductGenerationTime;
		this.numberOfShapes = subBlock.size();
		this.messageLength = MSG_LEN + numberOfShapes * GraphicMicroBustProductSubBlock.MSG_LEN;
	}

	@Override
	public byte[] toByteArray() {
		ByteBuffer br = ByteBuffer.allocate(messageLength);
		br.put(ByteUtil.to2Bytes(messageId));
		br.put(ByteUtil.to2Bytes(messageLength));
		br.put(ByteUtil.to4Bytes(mbProductGenerationTime));
		br.put(ByteUtil.EMPTY_1B);
		br.put(ByteUtil.toByte(numberOfShapes));
		br.put(ByteUtil.EMPTY_2B);
		for (GraphicMicroBustProductSubBlock sb : subBlock) {
			br.put(sb.toByteArray());
		}
		return br.array();
	}

	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append(messageId);
		buf.append(",");
		buf.append(messageLength);
		buf.append(",");
		buf.append(mbProductGenerationTime);
		buf.append(",");
		buf.append(numberOfShapes);
		buf.append("\nSubBlock::\n");
		for (GraphicMicroBustProductSubBlock sb : subBlock) {
			buf.append(sb.toString());
			buf.append("\n");
		}
		return buf.toString();
	}

	@Override
	public String getMessageTitle() {
		return TITLE;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public int getMessageLength() {
		return messageLength;
	}

	public void setMessageLength(int messageLength) {
		this.messageLength = messageLength;
	}

	public long getMbProductGenerationTime() {
		return mbProductGenerationTime;
	}

	public void setMbProductGenerationTime(long mbProductGenerationTime) {
		this.mbProductGenerationTime = mbProductGenerationTime;
	}

	public int getNumberOfShapes() {
		return numberOfShapes;
	}

	public void setNumberOfShapes(int numberOfShapes) {
		this.numberOfShapes = numberOfShapes;
	}

	public ArrayList<GraphicMicroBustProductSubBlock> getSubBlock() {
		return subBlock;
	}

	public void setSubBlock(ArrayList<GraphicMicroBustProductSubBlock> subBlock) {
		this.subBlock = subBlock;
	}
	public int getId() {
		return MSG_ID;
	}
}
